app.register.controller("UsstkMarketCtrl", ["$rootScope", "$scope", "$location", "$timeout", "$window", "ToastMessage", "SecurityService", "StockService", "QuotationService", "UserService", "$q", function($rootScope, $scope, $location, $timeout, $window, ToastMessage, SecurityService, StockService, QuotationService, UserService, $q) {
    function autoRefresh() {
        timer = setTimeout(function() {
            $scope.loadData()["finally"](autoRefresh)
        }, interval)
    }
    $scope.index = {},
    StockService.cache("morezfb_us", null ),
    StockService.cache("moredfb_us", null ),
    $scope.getDerict = function(index) {
        return index ? index.increase > 0 ? "+" : "-" : ""
    }
    ,
    $scope.getZde = function(index) {
        return index ? Math.abs(index.increase) : ""
    }
    ,
    $scope.getZdf = function(index) {
        return index ? Math.abs(index.increasePercent) : ""
    }
    ,
    $scope.hangQing = function(p) {
        $location.url("/usstk/timeshare?market=" + p.market + "&code=" + p.code)
    }
    ,
    $scope.getPrice = function(p) {
        return p.price ? p.price.toFixed(2) : "-"
    }
    ,
    $scope.getZf = function(p) {
        if (!p.price)
            return "-";
        if (0 == p.lastPrice)
            return "-";
        var inc = p.price - p.lastPrice;
        if (0 == inc)
            return "0.00%";
        var prec = inc / p.lastPrice
          , inderict = prec > 0 ? "+" : "";
        return prec = (100 * prec).toFixed(2),
        inderict + prec + "%"
    }
    ,
    $scope.toBuy = function(p) {
        $location.url("/usstk/buying?code=" + p.code)
    }
    ,
    $scope.refresh = function() {
        funs.pageLoad()
    }
    ;
    var funs = {
        pageLoad: function() {
            return $q.all([funs.getIndexs(), funs.getStockZfb(), funs.getStockDfb()])
        },
        getIndexs: function() {
            var searchList = [];
            searchList.push({
                code: "DJIA",
                stockMarket: "US_NASDAQ"
            }),
            searchList.push({
                code: "IXIC",
                stockMarket: "US_NASDAQ"
            }),
            searchList.push({
                code: "SPX",
                stockMarket: "US_NASDAQ"
            });
            var idxPromise = QuotationService.index({
                searchList: searchList
            });
            return idxPromise.success(function(data) {
                if (data && data.success && data.result)
                    for (var result = data.result, i = 0; i < result.length; i++) {
                        var r = result[i];
                        $scope.index["q" + r.code] = r
                    }
            }),
            idxPromise
        },
        getStockZfb: function() {
            var zfbPromise = QuotationService.boardStock("USA", "zgag");
            return zfbPromise.success(function(data) {
                data && data.success && ($scope.stockZfbs = data.result)
            }),
            zfbPromise
        },
        getStockDfb: function() {
            var dfbPromise = QuotationService.boardStock("USA", "zmmg");
            return dfbPromise.success(function(data) {
                $scope.stockDfbs = data.result
            }),
            dfbPromise
        }
    };
    $scope.loadData = funs.pageLoad;
    var timer = null 
      , interval = 5e3;
    $scope.loadData(),
    autoRefresh(),
    $scope.$on("$destroy", function() {
        clearTimeout(timer)
    })
}
]);
